package com.yx.shop.service.impl;

import com.yx.shop.common.Const;
import com.yx.shop.dao.UserDao;
import com.yx.shop.dao.impl.UserDaoImpl;
import com.yx.shop.entitly.User;
import com.yx.shop.service.UserService;
import com.yx.shop.utils.*;

/**
 * @author admin
 * @Classname UserServiceImpl
 * @Description TODO:
 * @Date 2021/05/06 16:59
 * @Created by yx
 */
public class UserServiceImpl implements UserService {

    private UserDao userDao = new UserDaoImpl();

    @Override
    public User getUserByName(String name) {
        User user = null;
        try {
            JDBCUtils.begin();
            user = userDao.getUserByName(name);
            JDBCUtils.commit();
        } catch (Exception e) {
            JDBCUtils.rollback();
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public boolean register(String username, String password, String email, String sex) {
        //封装数据,给密码加密
        User user = new User(username, MD5Utils.md5(password), email, sex);
        //设置余下数据,页面不展示该数据,却是必要且重要数据
        //ustatus 0 未激活 1 激活
        user.setUstatus(0);
        //ucode 邮件激活码  RandomUtils.createActive() 获取随机激活码
        user.setUcode(RandomUtils.createActive());
        //urole 用户 0 管理员 1 --->注册页面注册用户默认为普通用户
        user.setUrole(0);

        int result = userDao.register(user);

        if (result > 0) {
            //注册成功
            //发送注册邮件
            EmailUtils.sendEmail(user);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public int activate(String e, String c) {
        //1把e和c进行解密(解码)
        String email = Base64Utils.decode(e);
        String code = Base64Utils.decode(c);
        User user = userDao.getUserByEmail(email);
        //判断用户是否存在
        if (user != null) {
            //激活码正确
            if (user.getUcode().equals(code)) {
                //激活码正确,判断是否被激活
                if (user.getUstatus() != Const.UserInfo.STATUS_ACT) {
                    int result = userDao.setUstatus(user.getUid(), Const.UserInfo.ACT_SUCCESS);
                    if (result > 0) {
                        //状态修改成功--->激活成功
                        return Const.UserInfo.ACT_SUCCESS;
                    }
                } else {
                    //已经被激活,不需要重复激活
                    return Const.UserInfo.ACT_OK;
                }
            }
        }
        //激活失败
        return Const.UserInfo.ACT_FAIL;
    }

    @Override
    public User login(String username, String password) {
        User user = null;
        try {
            JDBCUtils.begin();
            //数据库中的密码经过加密,故需要将密码加密后再进行比较
            String md5Password = MD5Utils.md5(password);
            User temp = userDao.getUserByName(username);
            if (temp!=null&&temp.getUpassword().equals(md5Password)) {
                //用户存在且密码正确
                user=temp;
            }
            JDBCUtils.commit();
        } catch (Exception e) {
            JDBCUtils.rollback();
            e.printStackTrace();
        }
        return user;
    }
}
